#include "widgetdeviceform.h"
#include "ui_widgetdeviceform.h"
#include "../deviceClass/visibility.h"
#include "../deviceClass/metestation.h"
#include "../deviceClass/radiometer.h"

#include <QSerialPortInfo>

WidgetDeviceForm::WidgetDeviceForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WidgetDeviceForm)
{
    ui->setupUi(this);

    radioMeterTabInit();

    //能见度仪刷新串口按钮
    connect(ui->visSerialPortFleshbtn, &QPushButton::clicked, this, [=](){
        ui->visSerialPortcbx->clear();
        QList<QSerialPortInfo> info_list = QSerialPortInfo::availablePorts();
        QStringList portList;
        QListIterator<QSerialPortInfo> iter(info_list);
        iter.toFront();
        while (iter.hasNext()) {
            QSerialPortInfo info = iter.next();
            portList.append(info.portName());
        }
        if(portList.empty()){
            ui->visSerialPortcbx->addItem("没有串口");
        }else{
            ui->visSerialPortcbx->addItems(portList);
        }
    } );

    //能见度仪的打开串口
    connect(ui->visOpenClosebtn, &QPushButton::clicked, this, [=](){
        if(ui->visOpenClosebtn->text() == "连接"){
            QString port = ui->visSerialPortcbx->currentText();

            bool bl = Visibility::getInstance()->openDevice(port);
            if(!bl){
                qDebug()<<"打开失败";
            }
            else{
                /*启动的时候清空一下之前所有的点*/
                ui->visOpenClosebtn->setText("断开");
            }
        }else{
            Visibility::getInstance()->closeDevice();
            ui->visOpenClosebtn->setText("连接");
        }
    });

    //地面气象站的刷新串口
    connect(ui->meteSerialPortFleshbtn, &QPushButton::clicked, this, [=](){
        ui->meteSerialPortcbx->clear();
        QList<QSerialPortInfo> info_list = QSerialPortInfo::availablePorts();
        QStringList portList;
        QListIterator<QSerialPortInfo> iter(info_list);
        iter.toFront();
        while (iter.hasNext()) {
            QSerialPortInfo info = iter.next();
            portList.append(info.portName());
        }
        if(portList.empty()){
            ui->meteSerialPortcbx->addItem("没有串口");
        }else{
            ui->meteSerialPortcbx->addItems(portList);
        }
    });

    //地面气象站的打开和关闭
    connect(ui->meteOpenClosebtn, &QPushButton::clicked, this, [=](){
        if(ui->meteOpenClosebtn->text() == "连接"){
            QString port = ui->meteSerialPortcbx->currentText();

            bool bl = MeteStation::getInstance()->openDevice(port);
            if(!bl){
                qDebug()<<"打开失败";
            }
            else{
                /*启动的时候清空一下之前所有的点
                */
                ui->meteOpenClosebtn->setText("断开");
            }

        }else{
            MeteStation::getInstance()->closedevice();
            ui->meteOpenClosebtn->setText("连接");
        }
    });


}

//太阳辐射计页面初始化
void WidgetDeviceForm::radioMeterTabInit(){
    // 刷新串口
    connect(ui->btn_solorFelsehSerialPort, &QPushButton::clicked, this, [=](){
        ui->cbx_solorSerialPort->clear();
        QList<QSerialPortInfo> info_list = QSerialPortInfo::availablePorts();
        QStringList portList;
        QListIterator<QSerialPortInfo> iter(info_list);
        iter.toFront();
        while (iter.hasNext()) {
            QSerialPortInfo info = iter.next();
            portList.append(info.portName());
        }
        if(portList.empty()){
            ui->cbx_solorSerialPort->addItem("没有串口");
        }else{
            ui->cbx_solorSerialPort->addItems(portList);
        }
    });

    //打开和关闭串口
    connect(ui->btn_solorOpenClose, &QPushButton::clicked, this, [=](){
        if(ui->btn_solorOpenClose->text() == "连接"){
            QString port = ui->cbx_solorSerialPort->currentText();

            bool bl = RadioMeter::getInstance()->openDevice(port);
            if(!bl){
                qDebug()<<"打开失败";
            }
            else{
                /*启动的时候清空一下之前所有的点
                */
                ui->btn_solorOpenClose->setText("断开");
            }

        }else{
            RadioMeter::getInstance()->closeDevice();
            ui->btn_solorOpenClose->setText("连接");
        }
    });

    //STA状态查询更新
    connect(RadioMeter::getInstance(), &RadioMeter::updateView_STA, this, [=](const RadioMeterSTAProtocolAll& point){
        ui->edit_solorRadioMeterStatus->setText(point.radioMeterStatus);
        ui->edit_solorHeadTemperature->setText(QString::number(point.temperature));
        ui->edit_solorHeadHumidity->setText(QString::number(point.humidity));
        ui->edit_solorVoltage->setText(QString::number(point.voltage));
        ui->edit_solorErrCode->setText(point.errCode);
        ui->edit_solorHeighAngle->setText(QString::number(point.heightAngle));
        ui->edit_solorAzimuthAngle->setText(QString::number(point.azimuthAngle));
        ui->edit_solorTurnTableStatus->setText(point.turnTableStatus);
        ui->edit_solorPowerStatus->setText(point.powerStatus);
    });

    //查询按钮
    connect(ui->btn_solorQueryState, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        RadioMeter::getInstance()->m_port->write("SAT");
    });

    //重启按钮
    connect(ui->btn_solorRestart, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        RadioMeter::getInstance()->m_port->write("SystemReset");
    });

    //设置密集测量时间间隔
    connect(ui->btn_solorSetInter, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        QByteArray ary = "SetTimeSpan,";
        QString time = ui->spinBox_SolorSetInter->text();
        if(time.length() == 1){
            ary.append("0");
        }
        ary.append(ui->spinBox_SolorSetInter->text().toLatin1());
        RadioMeter::getInstance()->m_port->write(ary);
    });

    //查询纬度
    connect(ui->btn_solorGetLat, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        RadioMeter::getInstance()->m_port->write("Lat");
    });

    //查询经度
    connect(ui->btn_solorGetLon, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        RadioMeter::getInstance()->m_port->write("Lon");
    });

    //设置纬度
    connect(ui->btn_solorSetLat, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        QByteArray ary = "Lat,";
        //注意有空格
        if(ui->cbx_solorSetLat->currentText() == "    南纬"){
            ary.append("-");
        }
        ary.append(ui->spinBox_solorSetLat->text().toLatin1());
        RadioMeter::getInstance()->m_port->write(ary);
    });

    //设置经度
    connect(ui->btn_solorSetLon, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        QByteArray ary = "Lon,";
        //注意有空格
        if(ui->cbx_solorSetLon->currentText() == "    东经"){
            ary.append("-");
        }
        ary.append(ui->spinBox_solorSetLon->text().toLatin1());
        RadioMeter::getInstance()->m_port->write(ary);
    });

    //下载数据
    connect(ui->btn_solorDownloadData, &QPushButton::clicked, RadioMeter::getInstance(), [=](){
        QDateTime time1 = QDateTime::fromString(ui->edit_solorBeginTime->text(), "yyyy/MM/dd HH");
        QDateTime time2 = QDateTime::fromString(ui->edit_solorEndTime->text(), "yyyy/MM/dd HH");
        QString begin = time1.toString("MMddHH");
        QString end = time2.toString("MMddHH");

        QByteArray ary = "READDATA,";
        ary.append(begin.toLatin1());
        ary.append("-");
        ary.append(end.toLatin1());

        RadioMeter::getInstance()->m_port->write(ary);
    });

    //更新纬度
    connect( RadioMeter::getInstance(), &RadioMeter::updateView_LAT, this, [this](const QString& lat){
        ui->edit_solorGetLat->setText(lat);
    });

    //更新经度
    connect( RadioMeter::getInstance(), &RadioMeter::updateView_LON, this, [this](const QString& lon){
        ui->edit_solorGetLon->setText(lon);
    });
}

WidgetDeviceForm::~WidgetDeviceForm()
{
    delete ui;
}




